var materId = getQueryString("materId");
//系统状态
var flag =getQueryString("flag");
$(function () {
    var url = '../materialrecord/list?material=' + materId
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: '工装编码', name: 'spareCode', index: 'SPARE_CODE', width: 80},
            {label: '工装名称', name: 'spareName', index: 'SPARE_NAME', width: 80},
            {label: '仓库代码', name: 'storeCode', index: 'storeCode', width: 80},
            {label: '仓库名称', name: 'storeName', index: 'storeName', width: 80},
            {label: '备件位置', name: 'postion', index: 'postion', width: 80},
            {label: '领用数量', name: 'num', index: 'NUM', width: 80},
            {label: '领用部门', name: 'dept', index: 'DEPT', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
            {label: 'store', name: 'store', index: 'store',hidden: true},

        ]
    });
});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        materialRecord: {store: '', dept: '', material: materId, num: 1, spareCode: '', id: '',type: 1},
        ruleValidate: {
            spareCode: [
                {required: true, message: '工装不能为空', trigger: 'change'}
            ],
            num: [
                {required: true, type: 'number',message: '领取数量必须为数字且不能为空', trigger: 'change'}
            ]
        },
        q: {
            spareCode: ''
        },
        spareName:'',
        storeNum:0,
        locNum:0
    },
    methods: {
        query: function () {
            vm.reload();
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var nu = parseInt(vm.storeNum) - parseInt(vm.locNum)- parseInt(vm.materialRecord.num) ;
            // console.log(nu);
            if(flag==1){
                iview.Message.error("审核通过或者领料单结束之后禁止编辑！");
            }else {
                if (nu < 0) {
                    iview.Message.error('领取数量不能大于库存数量!');
                    return;
                } else {
                    if(vm.materialRecord.num>0){
                        let url = vm.materialRecord.id == (null || '') ? "../materialrecord/save" : "../materialrecord/update";
                        Ajax.request({
                            url: url,
                            params: JSON.stringify(vm.materialRecord),
                            type: "POST",
                            contentType: "application/json",
                            successCallback: function (r) {
                                alert('操作成功', function (index) {
                                    vm.reload();
                                });
                            }
                        });
                    }else {
                        iview.Message.error('领取数量不能为0!');
                        return;
                    }

                }
            }

        },
        del: function (event) {

            if(flag==1){
                iview.Message.error("审核通过或者领料单结束之后禁止编辑！");
            }else {

                let ids = getSelectedRows("#jqGrid");
                if (ids == null) {
                    return;
                }
                confirm('确定要删除选中的记录？', function () {
                    Ajax.request({
                        url: "../materialrecord/delete",
                        params: JSON.stringify(ids),
                        type: "POST",
                        contentType: "application/json",
                        successCallback: function () {
                            alert('操作成功', function (index) {
                                vm.reload();
                            });
                        }
                    });
                });
            }
        },

        reload: function (event) {
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'spareCode': vm.q.spareCode},
                page: page
            }).trigger("reloadGrid");
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        getToolsInfo:function(){
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择工装',
                content: '../tools/toolsstock_choose.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getToolsStroe();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        layer.close(index);

                        vm.materialRecord.spareCode = res.toolsCode;
                        vm.spareName = res.toolsName;
                        vm.storeNum = res.storeNum;
                        vm.locNum = res.locNum;
                        vm.materialRecord.postion = res.postion;
                        vm.materialRecord.store = res.storeId;
                        vm.materialRecord.postionId = res.id;
                        vm.materialRecord.spareName = vm.spareName;
                    }
                }
            })
        },
    }
});
